@@ -736,3 +736,98 @@ describe Agent do |
||
| 736 | 736 |
end |
| 737 | 737 |
end |
| 738 | 738 |
end |
| 739 |
+ |
|
| 740 |
+describe AgentDrop do |
|
| 741 |
+ def interpolate(string, agent) |
|
| 742 |
+ agent.interpolate_string(string, "agent" => agent) |
|
| 743 |
+ end |
|
| 744 |
+ |
|
| 745 |
+ before do |
|
| 746 |
+ @wsa1 = Agents::WebsiteAgent.new( |
|
| 747 |
+ name: 'XKCD', |
|
| 748 |
+ options: {
|
|
| 749 |
+ expected_update_period_in_days: 2, |
|
| 750 |
+ type: 'html', |
|
| 751 |
+ url: 'http://xkcd.com/', |
|
| 752 |
+ mode: 'on_change', |
|
| 753 |
+ extract: {
|
|
| 754 |
+ url: { css: '#comic img', attr: 'src' },
|
|
| 755 |
+ title: { css: '#comic img', attr: 'alt' },
|
|
| 756 |
+ }, |
|
| 757 |
+ }, |
|
| 758 |
+ schedule: 'every_1h', |
|
| 759 |
+ keep_events_for: 2) |
|
| 760 |
+ @wsa1.user = users(:bob) |
|
| 761 |
+ @wsa1.save! |
|
| 762 |
+ |
|
| 763 |
+ @wsa2 = Agents::WebsiteAgent.new( |
|
| 764 |
+ name: 'Dilbert', |
|
| 765 |
+ options: {
|
|
| 766 |
+ expected_update_period_in_days: 2, |
|
| 767 |
+ type: 'html', |
|
| 768 |
+ url: 'http://dilbert.com/', |
|
| 769 |
+ mode: 'on_change', |
|
| 770 |
+ extract: {
|
|
| 771 |
+ url: { css: '[id^=strip_enlarged_] img', attr: 'src' },
|
|
| 772 |
+ title: { css: '.STR_DateStrip', text: true },
|
|
| 773 |
+ }, |
|
| 774 |
+ }, |
|
| 775 |
+ schedule: 'every_12h', |
|
| 776 |
+ keep_events_for: 2) |
|
| 777 |
+ @wsa2.user = users(:bob) |
|
| 778 |
+ @wsa2.save! |
|
| 779 |
+ |
|
| 780 |
+ @efa = Agents::EventFormattingAgent.new( |
|
| 781 |
+ name: 'Formatter', |
|
| 782 |
+ options: {
|
|
| 783 |
+ instructions: {
|
|
| 784 |
+ message: '{{agent.name}}: {{title}} {{url}}',
|
|
| 785 |
+ agent: '{{agent.type}}',
|
|
| 786 |
+ }, |
|
| 787 |
+ mode: 'clean', |
|
| 788 |
+ matchers: [], |
|
| 789 |
+ skip_created_at: 'false', |
|
| 790 |
+ }, |
|
| 791 |
+ keep_events_for: 2, |
|
| 792 |
+ propagate_immediately: true) |
|
| 793 |
+ @efa.user = users(:bob) |
|
| 794 |
+ @efa.sources << @wsa1 << @wsa2 |
|
| 795 |
+ @efa.memory[:test] = 1 |
|
| 796 |
+ @efa.save! |
|
| 797 |
+ end |
|
| 798 |
+ |
|
| 799 |
+ it 'should be created via Agent#to_liquid' do |
|
| 800 |
+ @wsa1.to_liquid.class.should be(AgentDrop) |
|
| 801 |
+ @wsa2.to_liquid.class.should be(AgentDrop) |
|
| 802 |
+ @efa.to_liquid.class.should be(AgentDrop) |
|
| 803 |
+ end |
|
| 804 |
+ |
|
| 805 |
+ it 'should have .type and .name' do |
|
| 806 |
+ t = '{{agent.type}}: {{agent.name}}'
|
|
| 807 |
+ interpolate(t, @wsa1).should eq('WebsiteAgent: XKCD')
|
|
| 808 |
+ interpolate(t, @wsa2).should eq('WebsiteAgent: Dilbert')
|
|
| 809 |
+ interpolate(t, @efa).should eq('EventFormattingAgent: Formatter')
|
|
| 810 |
+ end |
|
| 811 |
+ |
|
| 812 |
+ it 'should have .options' do |
|
| 813 |
+ t = '{{agent.options.url}}'
|
|
| 814 |
+ interpolate(t, @wsa1).should eq('http://xkcd.com/')
|
|
| 815 |
+ interpolate(t, @wsa2).should eq('http://dilbert.com/')
|
|
| 816 |
+ interpolate('{{agent.options.instructions.message}}',
|
|
| 817 |
+ @efa).should eq('{{agent.name}}: {{title}} {{url}}')
|
|
| 818 |
+ end |
|
| 819 |
+ |
|
| 820 |
+ it 'should have .sources' do |
|
| 821 |
+ t = '{{agent.sources.size}}: {{agent.sources | map:"name" | join:", "}}'
|
|
| 822 |
+ interpolate(t, @wsa1).should eq('0: ')
|
|
| 823 |
+ interpolate(t, @wsa2).should eq('0: ')
|
|
| 824 |
+ interpolate(t, @efa).should eq('2: XKCD, Dilbert')
|
|
| 825 |
+ end |
|
| 826 |
+ |
|
| 827 |
+ it 'should have .receivers' do |
|
| 828 |
+ t = '{{agent.receivers.size}}: {{agent.receivers | map:"name" | join:", "}}'
|
|
| 829 |
+ interpolate(t, @wsa1).should eq('1: Formatter')
|
|
| 830 |
+ interpolate(t, @wsa2).should eq('1: Formatter')
|
|
| 831 |
+ interpolate(t, @efa).should eq('0: ')
|
|
| 832 |
+ end |
|
| 833 |
+end |